<?php

namespace Home\Controller; 
use Common\Controller\CommonController; 

class ProjectController extends CommonController {
	

	/**
	* 项目列表
	* @return
	*/
	function index(){

		$data=I('post.');
		
		if($data!=null){
			
			if($data['p_name']!=""){
				$where['p_name']=array('like','%'.$data['p_name'].'%');	
			}
			if($data['code']!=""){
				$where['code']=$data['code'];	
			}
			if($data['p_type']!=""){
				$where['p_type']=$data['p_type'];	
			}
				
			$count=M('project')->where($where)->count();
			$project=M("project")->where($where)->order('id desc')->select();
		}else{
			$count=M('project')->count();
			$project=M("project")->order('id desc')->select();
		}
		foreach($project as $li){
			$projectac=M("project_user")->where(array("p_id"=>$li['id']))->find();
			$user=M('user')->where(array("id"=>$li['user_id']))->find();
			
			$pm=M('user')->where(array("id"=>$projectac['pm_id']))->find();
			
			$pg_id= explode(',',$projectac['pg_id']); 
			$pg="";
			foreach ($pg_id as $k=>$id) {
				$pgname=M('user')->where(array("id"=>$id))->find();
				if($k>0){
					$pg.="/".$pgname['username'];
				}else{
					$pg.=$pgname['username'];
				}
			}
			$li['ctime']=date('Y-m-d',$li['ctime']);
			$li['offtime']=date('Y-m-d',$li['offtime']);
			switch ($li['p_type']) {
				case '1':
					$li['p_type']="App";
					break;
				case '2':
					$li['p_type']="微信公众号";
					break;
				case '3':
					$li['p_type']="web";
					break;
				case '4':
					$li['p_type']="小程序";
					break;
				case '5':
					$li['p_type']="H5";
					break;
				default:
					$li['p_type']="其他";
					break;
			}
			switch ($li['p_state']) {
				case '0':
					$li['p_state']="待跟进";
					break;
				case '1':
					$li['p_state']="已解决";
					break;
				case '2':
					$li['p_state']="结束";
					break;
				case '2':
					$li['p_state']="关闭";
					break;
				default:
					$li['p_state']="已过期";
					break;
			}
			if($li['ad']==0){
				$li['ad']="尚未开发"	;
			}
			$li['user_id']=$user['username'];
			$li['pm']=$pm['username'];
			$li['pg']=$pg;
			$arr[]=$li;
		}
		
		
		$page=new \Think\Pager($count,10);
		$show=$page->showhtml();// 分页显示输出
		$this->assign("list",$arr);
		$this->assign('page',$show);
		$this->display();
	}

	/**
	* 查看详情
	* @return
	*/
	function content(){
		$pid=I('id');
		$project=M("project")->where(array('id'=>$pid))->find();
			$projectac=M("project_user")->where(array("p_id"=>$pid))->find();
			$user=M('user')->where(array("id"=>$project['user_id']))->find();
			
			$pm=M('user')->where(array("id"=>$projectac['pm_id']))->find();
			
			$pg_id= explode(',',$projectac['pg_id']); 
			$pg="";
			foreach ($pg_id as $k=>$id) {
				$pgname=M('user')->where(array("id"=>$id))->find();
				if($k>0){
					$pg.="/".$pgname['username'];
				}else{
					$pg.=$pgname['username'];
				}
			}
			$project['ctime']=date('Y-m-d',$project['ctime']);
			$project['offtime']=date('Y-m-d',$project['offtime']);
			switch ($project['p_type']) {
				case '1':
					$project['p_type']="App";
					break;
				case '2':
					$project['p_type']="微信公众号";
					break;
				case '3':
					$project['p_type']="web";
					break;
				case '4':
					$project['p_type']="小程序";
					break;
				case '5':
					$project['p_type']="H5";
					break;
				default:
					$project['p_type']="其他";
					break;
			}
			switch ($project['p_state']) {
				case '0':
					$project['p_state']="待跟进";
					break;
				case '1':
					$project['p_state']="已解决";
					break;
				case '2':
					$project['p_state']="结束";
					break;
				case '2':
					$project['p_state']="关闭";
					break;
				default:
					$project['p_state']="已过期";
					break;
			}
			if($project['ad']==0){
				$project['ad']="尚未开发"	;
			}
			$project['user_id']=$user['username'];
			$project['pm']=$pm['username'];
			$project['pg']=$pg;
			$this->assign("li",$project);
			$this->display();
		
	}

	/**
	* 获取用户
	* @return
	*/
	
	function getuser(){
		$data=I('get.');
		if($data!=null){
			$where['username']= array('like','%'.$data['name'].'%');
			
			$count=M('user')->where($where)->count();
			$list=M("user")->where($where)->select();
		}else{
			$count=M('user')->count();
			$list=M("user")->select();
		}
		$page=new \Think\Pager($count,15);
		$shows=$page->showhtml();// 分页显示输出
		$this->assign("pid",$data['id']);
		$this->assign("list",$list);
		$this->assign('page',$shows);
		$this->display("user");
		
	}
	
	/**
	* 审核
	* @return
	*/
	
	function audit(){
		$data=I('post.');
		
		$where=array('id'=>$data['pid']);
		if($data['st']==1){
			$save=array(
			'status'=>$data['st'],
			);
			
		}else{
			$save=array(
			'status'=>$data['st'],
			'off_ds'=>$data['con']
			);
		}		
		try{
			$res=M("project")->where($where)->save($save);
			$this->ajaxReturn(array('s'=>true));
		}catch( Exception $e){
			$this->ajaxReturn(array('s'=>false));
		}
		
	}
	
	/**
	* 指定项目经理
	* @return
	*/
	
	function savepm(){
		$data=I('post.');
		$where=array('p_id'=>$data['pid']);
		$res=M('project_user')->where($where)->find();
		if($res){
			$save=array(
			'pm_id'=>$data['uid']
			);
			try{
				$rest=M("project_user")->where($where)->save($save);
				$this->ajaxReturn(array('s'=>true));
			}catch( Exception $e){
				$this->ajaxReturn(array('s'=>false));
			}
		}else{
			$add=array(
			'p_id'=>$data['pid'],
			'pm_id'=>$data['uid']
			);
			$rest=M("project_user")->add($add);
			if($rest){
				$this->ajaxReturn(array('s'=>true));
			}
		};
	}
}